package com.atguigu.teach.mytask;

import java.util.concurrent.*;

public class MyThreadPoolExecutor {
    public static void main(String[] args) {
//        ExecutorService threadpool= Executors.newFixedThreadPool(5);//一池五线程
//        //ExecutorService threadpool= Executors.newSingleThreadExecutor();//一池1线程
//        //ExecutorService threadpool= Executors.newCachedThreadPool();//一池n线程,扩容
//
//        try {
//            //20个线程来该银行办理业务，目前有5个工作人员
//            for (int i = 1; i <=20 ; i++) {
//                int finaI=i;
//                threadpool.submit(()->{
//                    System.out.println(Thread.currentThread().getName()+"\t"+"为顾客："+finaI+"办理业务");
//                });
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            //关闭
//            threadpool.shutdown();
//        }

//        自己写线程池

        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor=new ScheduledThreadPoolExecutor(5);

        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
                );

                try {
            //20个线程来该银行办理业务，目前有5个工作人员
            for (int i = 1; i <=20 ; i++) {
                int finaI=i;
                threadPoolExecutor.submit(()->{
                    System.out.println(Thread.currentThread().getName()+"\t"+"为顾客："+finaI+"办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭
                    threadPoolExecutor.shutdown();
        }
    }



}
